package com.company.oa.hr.service.training.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.oa.common.exception.BusinessException;
import com.company.oa.hr.dto.training.CourseDTO;
import com.company.oa.hr.entity.training.Course;
import com.company.oa.hr.entity.training.Plan;
import com.company.oa.hr.mapper.training.CourseMapper;
import com.company.oa.hr.service.training.CourseService;
import com.company.oa.hr.service.training.PlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private PlanService planService;

    @Override
    public IPage<Map<String, Object>> getCoursePage(int page, int size, Course course) {
        Page<Course> pageParam = new Page<>(page, size);
        return baseMapper.selectCoursePageWithPlan(pageParam, course);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createCourse(CourseDTO courseDTO) {
        // 检查培训计划是否存在
        Plan plan = planService.getById(courseDTO.getPlanId());
        if (plan == null) {
            throw new BusinessException("培训计划不存在");
        }
        
        // 只有已发布状态的培训计划可以添加课程
        if (plan.getStatus() != 2) {
            throw new BusinessException("只有已发布状态的培训计划可以添加课程");
        }
        
        Course course = new Course();
        BeanUtils.copyProperties(courseDTO, course);
        
        // 设置状态为未开始
        course.setStatus(1);
        course.setCurrentParticipants(0);
        course.setCreateTime(new Date());
        
        return save(course);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCourse(CourseDTO courseDTO) {
        // 检查课程是否存在
        Course existCourse = getById(courseDTO.getId());
        if (existCourse == null) {
            throw new BusinessException("课程不存在");
        }
        
        // 已开始或已结束的课程不能修改基本信息
        if (existCourse.getStatus() != 1) {
            throw new BusinessException("已开始或已结束的课程不能修改基本信息");
        }
        
        Course course = new Course();
        BeanUtils.copyProperties(courseDTO, course);
        
        // 设置更新时间
        course.setUpdateTime(new Date());
        
        return updateById(course);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCourse(Long id) {
        // 检查课程是否存在
        Course existCourse = getById(id);
        if (existCourse == null) {
            throw new BusinessException("课程不存在");
        }
        
        // 已开始或已结束的课程不能删除
        if (existCourse.getStatus() != 1) {
            throw new BusinessException("已开始或已结束的课程不能删除");
        }
        
        return removeById(id);
    }

    @Override
    public List<Course> getPlanCourses(Long planId) {
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Course::getPlanId, planId)
                   .orderByAsc(Course::getStartTime);
        return list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean startCourse(Long id) {
        // 检查课程是否存在
        Course existCourse = getById(id);
        if (existCourse == null) {
            throw new BusinessException("课程不存在");
        }
        
        // 只有未开始状态的课程可以开始
        if (existCourse.getStatus() != 1) {
            throw new BusinessException("只有未开始状态的课程可以开始");
        }
        
        Course course = new Course();
        course.setId(id);
        course.setStatus(2); // 进行中状态
        course.setUpdateTime(new Date());
        
        return updateById(course);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean endCourse(Long id) {
        // 检查课程是否存在
        Course existCourse = getById(id);
        if (existCourse == null) {
            throw new BusinessException("课程不存在");
        }
        
        // 只有进行中状态的课程可以结束
        if (existCourse.getStatus() != 2) {
            throw new BusinessException("只有进行中状态的课程可以结束");
        }
        
        Course course = new Course();
        course.setId(id);
        course.setStatus(3); // 已结束状态
        course.setUpdateTime(new Date());
        
        return updateById(course);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean cancelCourse(Long id) {
        // 检查课程是否存在
        Course existCourse = getById(id);
        if (existCourse == null) {
            throw new BusinessException("课程不存在");
        }
        
        // 已结束的课程不能取消
        if (existCourse.getStatus() == 3) {
            throw new BusinessException("已结束的课程不能取消");
        }
        
        Course course = new Course();
        course.setId(id);
        course.setStatus(4); // 已取消状态
        course.setUpdateTime(new Date());
        
        return updateById(course);
    }
} 